package alg20241122;

public class Text {
    public int rob(int[] nums){
        int n = nums.length;
        int result1 =nums[0] + rob1(2,n-2,nums);
        int result2 = rob1(1,n-1,nums);
        return  Math.max(result1,result2);
    }
    public int rob1(int left,int right,int[] nums) {
       if(left > right) return 0;
        int n = nums.length;
        //1 创建表
        int[] f = new int[n];
        int[] g = new int[n];
        //2 初始化
        f[left] = nums[left];

        //3 填表
        for (int i = left + 1; i <= right; i++) {
            f[i] = g[i - 1] + nums[i];
            g[i] = Math.max(f[i - 1], g[i - 1]);
        }
        //4 返回值
        return Math.max(f[right], g[right]);
    }
}
